package org.example.campsys.mapper;

import org.apache.ibatis.annotations.*;
import org.example.campsys.model.*;

import java.util.List;

@Mapper
public interface TeacherMapper {

    @Insert("insert into t_course (course_name, created_time, status, activity_id, teacher_id, class_id) " +
            "values ( #{courseName}, #{createdTime}, 0, #{activityId}, #{teacherId}, #{classId})")
    Integer addCourse(CourseInfo courseInfo);

    @Select("<script> select id, course_name, created_time, status, activity_id, teacher_id, class_id from t_course" +
            "<where>" +
            "<if test = 'courseName != null'> and course_name = #{courseName} </if>" +
            "<if test = 'createdTime != null'> and created_time = #{createdTime} </if>" +
            "<if test = 'status != null'> and status = #{status} </if>" +
            "<if test = 'activityId != null'> and activity_id = #{activityId} </if>" +
            "<if test = 'teacherId != null'> and teacher_id = #{teacherId} </if>" +
            "<if test = 'classId != null'> and class_id = #{classId} </if>" +
            "</where>" +
            "</script>")
    List<CourseInfo> selCourse (CourseInfo courseInfo);

    @Delete("delete from t_course where id = #{id}")
    Integer delCourse(Integer id);

    @Update("<script>" +
            "update t_course <set>" +
            "<if test = 'courseName != null'> course_name = #{courseName}, </if>" +
            "<if test = 'createdTime != null'> created_time = #{createdTime}, </if>" +
            "<if test = 'status != null'> status = #{status}, </if>" +
            "<if test = 'activityId != null'> activity_id = #{activityId}, </if>" +
            "<if test = 'teacherId != null'> teacher_id = #{teacherId}, </if>" +
            "<if test = 'classId != null'> class_id = #{classId} </if> </set> where id = #{id}" +
            "</script>")
    Integer updCourse(CourseInfo courseInfo);

    @Update("update t_course set status = #{status} where id = #{id}")
    Integer orcCourse(CourseInfo courseInfo);

    @Select("select t_manager_student.id, t_manager_student.stu_name, t_manager_student.from_place, t_manager_student.parent_phone, t_manager_student.current_grade, " +
            "t_manager_student.is_first, t_manager_student.login_id, t_manager_student.comment, t_manager_student.status " +
            "from t_manager_student inner join t_teaching_student_in_class on t_manager_student.id = t_teaching_student_in_class.student_id " +
            "where t_teaching_student_in_class.class_id = #{classId}")
    List<StudentInfo> selClassStudent(Integer classId);


    @Insert("insert into t_course_learn_unit (unit_title, doc_path, video_path, unit_introduce, father_id, is_chpt, course_id, learning_score, status) " +
            "values (#{unitTitle}, #{docPath}, #{videoPath}, #{unitIntroduce}, #{fatherId}, #{isChpt}, #{courseId}, #{learningScore}, 0)")
    Integer addUnit(UnitInfo unitInfo);

    @Update("update t_course_learn_unit set status = #{status} where id = #{id}")
    Integer orcUnit(UnitInfo unitInfo);

    @Update("<script>" +
            "update t_course_learn_unit <set>" +
            "<if test = 'unitTitle != null'> unit_title = #{unitTitle}, </if>" +
            "<if test = 'docPath != null'> doc_path = #{docPath}, </if>" +
            "<if test = 'videoPath != null'> video_path = #{videoPath}, </if>" +
            "<if test = 'unitIntroduce != null'> unit_introduce = #{unitIntroduce}, </if>" +
            "<if test = 'fatherId != null'> father_id = #{fatherId}, </if>" +
            "<if test = 'isChpt != null'> is_chpt = #{isChpt}, </if>" +
            "<if test = 'courseId != null'> course_id = #{courseId}, </if>" +
            "<if test = 'learningScore != null'> learning_score = #{learningScore}, </if> " +
            "<if test = 'status != null'> status = #{status} </if> </set> where id = #{id}" +
            "</script>")
    Integer updUnit(UnitInfo unitInfo);

    @Delete("delete from t_course_learn_unit where id = #{id}")
    Integer delUnit(Integer id);

    @Select("<script>" +
            "select id, unit_title, doc_path, video_path, unit_introduce, father_id, is_chpt, course_id, learning_score, status from t_course_learn_unit" +
            "<where>" +
            "<if test = 'unitTitle != null'> and unit_title = #{unitTitle} </if>" +
            "<if test = 'docPath != null'> and doc_path = #{docPath} </if>" +
            "<if test = 'videoPath != null'> and video_path = #{videoPath} </if>" +
            "<if test = 'unitIntroduce != null'> and unit_introduce = #{unitIntroduce} </if>" +
            "<if test = 'fatherId != null'> and father_id = #{fatherId} </if>" +
            "<if test = 'isChpt != null'> and is_chpt = #{isChpt} </if>" +
            "<if test = 'courseId != null'> and course_id = #{courseId} </if>" +
            "<if test = 'learningScore != null'> and learning_score = #{learningScore} </if>" +
            "<if test = 'status != null'> and status = #{status} </if>" +
            "</where> " +
            "</script>")
    List<UnitInfo> selUnit(UnitInfo unitInfo);

    @Select("<script>" +
            "select id, stu_in_class_id, unit_id, finished_status, finished_time from t_teaching_student_learning" +
            "<where>" +
            "<if test = 'unitTitle != null'> and stu_in_class_id = #{stuInClassId} </if>" +
            "<if test = 'unitTitle != null'> and unit_id = #{unitId} </if>" +
            "<if test = 'unitTitle != null'> and finished_status = #{finishedStatus} </if>" +
            "<if test = 'unitTitle != null'> and finished_time = #{finishedTime} </if>" +
            "</where>" +
            "</script>")
    List<LearningInfo> selStudentUnit(LearningInfo learningInfo);

    @Insert("insert into t_teaching_evaluation (stu_in_class_id, grade, evaluation, evaluation_time, course_id, evaluator)" +
            "values (#{stuInClassId}, #{grade}, #{evaluation}, #{evaluationTime}, #{courseId}, #{evaluator})")
    Integer evaluate(EvaluationInfo evaluationInfo);

    @Insert("insert into t_teaching_student_learning (stu_in_class_id, unit_id) values (#{stuInClassId}, #{unitId})")
    Integer postTask(LearningInfo learningInfo);

    @Select("select t_course.id,t_course.course_name, t_course.created_time, t_course.status, t_course.activity_id, t_course.class_id" +
            " from t_course inner join t_manager_teacher on t_course.teacher_id = t_manager_teacher.id where t_manager_teacher.login_id = #{loginId}")
    List<CourseInfo> selMyCourse(Integer loginId);


}
